/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.indexer.worker;

import cn.ac.iie.ulss.indexer.runenvs.DefaultConstants;
import cn.ac.iie.ulss.indexer.runenvs.GlobalParas;
import iie.metastore.MetaStoreClient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Index;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;

/**
 *
 *
 */
public class InitMetastoreInfo implements Runnable {

    public static Logger log = Logger.getLogger(IndexControler.class.getName());
    private static List<String> databases = null;
    private static String tempTableType = null;
    private static String tempKeySeperator = null;

    public InitMetastoreInfo(List<String> dbs) {
        databases = dbs;
    }

    @Override
    public void run() {
        MetaStoreClient msc = GlobalParas.clientPool.getClient();
        for (String dbName : databases) {
            try {
                List<String> tbList = msc.client.getAllTables(dbName);
                GlobalParas.dbName2tableName.put(dbName, tbList);
                log.info("the table list size for " + dbName + " is " + tbList.size());
                for (String tbName : tbList) {
                    try {
                        Table tb = msc.client.getTable(dbName, tbName);
                        List<Index> idxList = msc.client.listIndexes(dbName, tbName, (short) -1);
                        List<FieldSchema> list = msc.client.getFields(dbName, tbName);
                        ArrayList<String> hbaseCols = new ArrayList<String>();
                        for (FieldSchema fieldSchema : list) {
                            hbaseCols.add(fieldSchema.getName());
                        }
                        ArrayList<String> keyColumns = new ArrayList<String>();
                        if (tb.getParameters().get("genKey") == null || tb.getParameters().get("genKey").equalsIgnoreCase("false")) {
                            GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "false");
                        } else if (tb.getParameters().get("genKey").equalsIgnoreCase("true")) {
                            GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "true");
                        }
                        if ((tempTableType = tb.getParameters().get("datastore")) != null) {
                            GlobalParas.tableTypes.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, tempTableType);
                        }
                        if ((tempKeySeperator = tb.getParameters().get("keySeperator")) != null) {
                            keyColumns.addAll(Arrays.asList(tb.getParameters().get("key").split("[" + tempKeySeperator + "]")));
                        }
                        GlobalParas.hbaseKeyColumns.put(dbName + "." + tbName, keyColumns);
                        GlobalParas.hbaseStoreColumns.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, hbaseCols);
                        GlobalParas.tablename2Table.put(dbName + "." + tbName, tb);
                        GlobalParas.tablename2indexs.put(dbName + "." + tbName, idxList);
                        log.info("get " + dbName + "." + tbName + " ok ");
                    } catch (Exception ex) {
                        continue;
                    }
                }
            } catch (Exception ex) {
                log.error(ex, ex);
            } finally {
                GlobalParas.clientPool.realseOneClient(msc);
            }

        }
    }

    public static List<String> getNewDB(String dbName) {
        MetaStoreClient msc = GlobalParas.clientPool.getClient();
        List<String> tbList = null;
        try {
            tbList = msc.client.getAllTables(dbName);
            GlobalParas.dbName2tableName.put(dbName, tbList);
            log.info("the table list size for " + dbName + " is " + tbList.size());
            for (String tbName : tbList) {
                try {
                    Table tb = msc.client.getTable(dbName, tbName);
                    List<Index> idxList = msc.client.listIndexes(dbName, tbName, (short) -1);
                    List<FieldSchema> list = msc.client.getFields(dbName, tbName);
                    ArrayList<String> hbaseCols = new ArrayList<String>();
                    for (FieldSchema fieldSchema : list) {
                        hbaseCols.add(fieldSchema.getName());
                    }
                    ArrayList<String> keyColumns = new ArrayList<String>();
                    if (tb.getParameters().get("genKey") == null || tb.getParameters().get("genKey").equalsIgnoreCase("false")) {
                        GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "false");
                    } else if (tb.getParameters().get("genKey").equalsIgnoreCase("true")) {
                        GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "true");
                    }
                    if ((tempTableType = tb.getParameters().get("datastore")) != null) {
                        GlobalParas.tableTypes.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, tempTableType);
                    }
                    if ((tempKeySeperator = tb.getParameters().get("keySeperator")) != null) {
                        keyColumns.addAll(Arrays.asList(tb.getParameters().get("key").split("[" + tempKeySeperator + "]")));
                    }
                    GlobalParas.hbaseKeyColumns.put(dbName + "." + tbName, keyColumns);
                    GlobalParas.hbaseStoreColumns.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, hbaseCols);
                    GlobalParas.tablename2Table.put(dbName + "." + tbName, tb);
                    GlobalParas.tablename2indexs.put(dbName + "." + tbName, idxList);
                    log.info("get " + dbName + "." + tbName + " ok ");
                } catch (Exception ex) {
                    continue;
                }
            }
        } catch (Exception ex) {
            log.error(ex, ex);
        } finally {
            GlobalParas.clientPool.realseOneClient(msc);
        }
        GlobalParas.dbName2tableName.put(dbName, tbList);
        return tbList;
    }

    public static void getMsHbaseInfo(String dbName, String tbName) {
        MetaStoreClient msc = GlobalParas.clientPool.getClient();
        try {
            Table tb = msc.client.getTable(dbName, tbName);
            List<FieldSchema> list = msc.client.getFields(dbName, tbName);
            ArrayList<String> hbaseCols = new ArrayList<String>();
            for (FieldSchema fieldSchema : list) {
                hbaseCols.add(fieldSchema.getName());
            }
            ArrayList<String> keyColumns = new ArrayList<String>();
            if (tb.getParameters().get("genKey") == null || tb.getParameters().get("genKey").equalsIgnoreCase("false")) {
                GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "false");
            } else if (tb.getParameters().get("genKey").equalsIgnoreCase("true")) {
                GlobalParas.tableName2GenKey.put(dbName + "." + tbName, "true");
            }
            if ((tempTableType = tb.getParameters().get("datastore")) != null) {
                GlobalParas.tableTypes.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, tempTableType);
            }
            if ((tempKeySeperator = tb.getParameters().get("keySeperator")) != null) {
                keyColumns.addAll(Arrays.asList(tb.getParameters().get("key").split("[" + tempKeySeperator + "]")));
            }
            GlobalParas.hbaseKeyColumns.put(dbName + "." + tbName, keyColumns);
            GlobalParas.hbaseStoreColumns.put(dbName + DefaultConstants.tableFullnNameDelimiter + tbName, hbaseCols);
            GlobalParas.tablename2Table.put(dbName + "." + tbName, tb);
            List<String> tbList = msc.client.getAllTables(dbName);
            GlobalParas.dbName2tableName.put(dbName, tbList);
        } catch (Exception ex) {
            log.info(ex, ex);
        } finally {
            GlobalParas.clientPool.realseOneClient(msc);
        }
    }

    /*
     * ConcurrentHashMap<String, LinkedHashMap<String, List<FieldSchema>>> table2idx2colMap = new ConcurrentHashMap<String, LinkedHashMap<String, List<FieldSchema>>>(50, 0.8f);
     ConcurrentHashMap<String, LinkedHashMap<String, Field>> table2idx2LuceneFieldMap = table2idx2LuceneFieldMap = new ConcurrentHashMap<String, LinkedHashMap<String, Field>>(50, 0.8f);
     ConcurrentHashMap<String, LinkedHashMap<String, FieldType>> table2idx2LuceneFieldTypeMap = table2idx2LuceneFieldTypeMap = new ConcurrentHashMap<String, LinkedHashMap<String, FieldType>>(50, 0.8f);
     */
        //InitEnvTools.initIndexinfo(null, null, null, dbName, tbName);
    //log.info("init index information for " + dbName + "." + tbName + " ok ");
    // try {
    // log.info(Indexer.table2idx2LuceneFieldTypeMapPool.take().toString());
    // } catch (InterruptedException ex) {
    // java.util.logging.Logger.getLogger(Initmetainfo.class.getName()).log(Level.SEVERE, null, ex);
    // }
}
